# -*- coding: utf-8 -*-
import os
from qq_2 import qqExtract,insertQqTable
from address_2 import addressExtract,insertAddressTable
from email_2 import emailExtract,insertEmailTable
from mobilphone_2 import mobelphoneNumberExtract,insertPhoneTable
from telphone_2 import telphoneNumExtract,insertTelphoneTable,IncludeSwitchNum,telphoneNumExtractList
from url_2 import urlExtract,insertUrlTable
from weichat_2 import weichatExtract,insertWeichatTable
from myUtil import switchNum2Zhongwen
from ETL.telphone_2 import quhaoInfo


def qq(workdir,fileList,filenameMd5):
    target=filenameMd5+'qq.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        qqres=[]
        for line in res:
            temp=line.strip()
            if len(temp)!=0 and isNotICP(temp):
                # print qqExtract(line)
                qqres+=qqExtract(temp)
                # qqres.append(qqExtract(line))
                # insertQqTable(line)
            else:
                continue
        return qqres
    else:
        return []

def address(workdir,fileList,filenameMd5):
    target=filenameMd5+'address.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        addressres=[]
        for line in res:
            if len(line.strip())!=0:
                # print addressExtract(line)
                # insertAddressTable(line)
                temp=addressExtract(line)
                if temp != None:
                    addressres.append(temp)
                # addressres+=addressExtract(line)
            else:
                continue
        return addressres
    else:
        return []

def email(workdir,fileList,filenameMd5):
    target=filenameMd5+'email.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        emailres=[]
        for line in res:
            if '@' in line:
                # print emailExtract(line)
                # insertEmailTable(line)
                # emailres.append(emailExtract(line))
                # 由于需要处理中文域名的数据，所以处理对象需要转换成unicode编码
                emailres+=emailExtract(unicode(line))
            else:
                continue
        return emailres
    else:
        return []

def mobilphone(workdir,fileList,filenameMd5):
    target=filenameMd5+'phone.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        phone=[]
        for line in res:
            temp=line.strip()
            if len(temp)!=0 and isNotICP(temp):
                # print mobelphoneNumberExtract(line)
                # insertPhoneTable(line)
                # phone.append(mobelphoneNumberExtract(line))
                phone+=mobelphoneNumberExtract(temp)
            else:
                continue
        return phone
    else:
        return []

def telphone(workdir,fileList,filenameMd5):
    target=filenameMd5+'telphone.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        telphoneres=[]

        mobilphoneres=[]
        for line in res:
            if len(line.strip())!=0 and isNotICP(line.strip()):
                line1=IncludeSwitchNum(line.strip())
                temp=mobelphoneNumberExtract(line1)
                if (line1.startswith('1') and line1.isdigit() and len(line1)==11):
                    mobilphoneres.append(line1)
                elif len(temp)!=0:
                    mobilphoneres+=temp
                else:
                    # telphoneList.append(temp)
                    # 按照最新流程处理座机号
                    telphoneres+=telphoneNumExtract(line1)  #完成对国家码的处理
            else:
                continue
        #返回前需要对座机号进行统一处理
        quhaoFlag=None
        resultTelphone=[]

        for item in telphoneres:
            if '-' in item:
                linshi=item.split('-')
                for key,value in quhaoInfo.items():
                    if key in linshi[0] and linshi[0] in value and (len(linshi[1])==7 or len(linshi[1])==8):
                        quhaoFlag=linshi[0]
                        resultTelphone.append(item)
                        break
            elif (len(item)==7 or len(item)==8) and quhaoFlag !=None:
                resultTelphone.append(quhaoFlag+"-"+item)
            elif len(item)==4:
                for key,value in quhaoInfo.items():
                    if key in item and item in value:
                        quhaoFlag=item
            elif len(item)==11 or len(item)==12:
                # resultTelphone.append(item)
                for key,value in quhaoInfo.items():
                    if key in item:
                        for oneV in value:
                            if oneV in item:
                                quhaoFlag=oneV
                                resultTelphone.append(quhaoFlag+"-"+item[len(quhaoFlag):])
                                break
                        break
            else:
                continue
        return resultTelphone,mobilphoneres
    else:
        return [],[]

def url(workdir,fileList,filenameMd5):

    target=filenameMd5+'url.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        urlres=[]
        for line in res:
            if ("http" in line or 'www' in line or "https" in line) and len(line.strip())>5:
                # print "----------------------------"
                # print line
                # print "----------------------------"
                # print urlExtract(line)
                # insertUrlTable(line)
                # urlres.append(urlExtract(line))
                urlres+=urlExtract(line)
            else:
                continue
        return urlres
    else:
        return []

def weichat(workdir,fileList,filenameMd5):
    target=filenameMd5+'weichat.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        weichatres=[]

        result=[]
        for line in res:

            if len(line.strip())!=0 and isNotICP(line.strip()):
                # print "+++++++++++++++++++++++++++++++++"
                # print line.strip()
                # print "+++++++++++++++++++++++++++++++++"
                # print weichatExtract(line)
                # insertWeichatTable(line)
                # weichatres.append(weichatExtract(line))
                weichatres+=weichatExtract(unicode(line.strip()))
            else:
                continue
        # print "================================="
        # print weichatres
        # print "================================="
        # 对提取的结果进行处理
        for each in weichatres:
            item=each.strip()
            if len(item)>=3:
                item=item.replace(':','')
                result.append(item)
                return result
            # else:
            #     continue
        return []
        # return weichatres
    else:
        return []

# 对特例模块进行处理
def otherHandle(workdir,fileList,filenameMd5):
    target=filenameMd5+'other.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        result={'email':[],'address':[],'mobilphone':[],'telphone':[],'url':[]}
        for line in res:
            each=line.strip()
            if len(each)!=0 and isNotICP(each):
                #-------------------------------------------
                address=addressExtract(each)    #存储的是字符串类型
                url=urlExtract(each)       #存储的是列表类型
                email=emailExtract(each)    #存储的是列表类型
                mobilphone=mobelphoneNumberExtract(line)

                line1=IncludeSwitchNum(each)
                if line1.startswith('1') and line1.isdigit() and len(line1)==11:
                    mobilphone.append(line1)
                else:
                    # telphoneList.append(temp)
                    # telphoneres+=telphoneNumExtract(line1)
                    telphone=telphoneNumExtract(line1)

                if address !=None:
                    result['address'].append(address)
                result['url']+=url
                result['email']+=email
                result['mobilphone']+=mobilphone
                result['telphone']+=telphone
                #-------------------------------------------
            else:
                continue
        return result
    else:
        return {'email':[],'address':[],'mobilphone':[],'telphone':[],'url':[]}


def companyName(workdir,fileList,filenameMd5):
    target=filenameMd5+'companyName.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()

        temp=[]
        for each in res:
            if len(each.strip())<8:
                continue
            temp.append(switchNum2Zhongwen(each))
        return temp
    else:
        return []


def isNotICP(string):
    if 'icp' in string or 'ICP' in string:
        return False
    else:
        return True